package com.zycfc.zsf.boot.util.socket;

import java.net.*;
import java.io.*;
import org.slf4j.*;

public class ClientSocketUtil
{
    private static final Logger log;
    protected static Socket socket;
    private boolean isConnected;
    
    public ClientSocketUtil() {
        this.isConnected = true;
    }
    
    public ClientSocketUtil(final int port) {
        this.isConnected = true;
        try {
            ClientSocketUtil.socket = new Socket(InetAddress.getLocalHost(), port);
        }
        catch (Exception e) {
            this.isConnected = false;
        }
    }
    
    public String send(final String ip, final int port, final int timeout, final String reqData) {
        return this.send(ip, port, timeout, "UTF-8", reqData);
    }
    
    public String send(final String ip, final int port, final int timeout, final String charsetName, final String reqData) {
        ClientSocketUtil.log.info("ip=" + ip + ",port=" + port + ",timeout=" + timeout);
        DataOutputStream out = null;
        InputStream in = null;
        Socket socket1 = null;
        try {
            socket1 = new Socket(ip, port);
            socket1.setSoTimeout(timeout);
            out = new DataOutputStream(socket1.getOutputStream());
            in = socket1.getInputStream();
            final byte[] msg = reqData.toString().getBytes("utf-8");
            String msgLen = msg.length + "";
            final int lenLimit = 6;
            if (msgLen.length() > lenLimit) {
                ClientSocketUtil.log.error("\u8d85\u957f," + msgLen.length() + "," + lenLimit);
                throw new IllegalArgumentException();
            }
            for (int i = msgLen.length() + 1; i <= lenLimit; ++i) {
                msgLen = "0" + msgLen;
            }
            ClientSocketUtil.log.info("\u8bf7\u6c42\u62a5\u6587\u957f\u5ea6=" + msgLen);
            ClientSocketUtil.log.info(reqData);
            out.write(msgLen.getBytes());
            out.write(msg);
            out.flush();
            final String respBody = readStream(in);
            return respBody;
        }
        catch (IOException e) {
            ClientSocketUtil.log.error("\u901a\u8baf\u5f02\u5e3801", (Throwable)e);
            throw new RuntimeException("socket\u901a\u8baf\u5f02\u5e38", e);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (IOException e2) {
                    ClientSocketUtil.log.error("\u901a\u8baf\u5f02\u5e3802", (Throwable)e2);
                    throw new RuntimeException("socket\u901a\u8baf\u5f02\u5e38", e2);
                }
            }
            if (out != null) {
                try {
                    out.close();
                }
                catch (IOException e2) {
                    ClientSocketUtil.log.error("\u901a\u8baf\u5f02\u5e3803", (Throwable)e2);
                    throw new RuntimeException("socket\u901a\u8baf\u5f02\u5e38", e2);
                }
            }
            try {
                if (socket1 != null) {
                    socket1.close();
                }
            }
            catch (IOException e2) {
                ClientSocketUtil.log.error("socket\u5173\u95ed\u5f02\u5e38", (Throwable)e2);
                throw new RuntimeException("socket\u901a\u8baf\u5f02\u5e38", e2);
            }
        }
    }
    
    public void close() {
        try {
            if (ClientSocketUtil.socket != null && ClientSocketUtil.socket.isConnected()) {
                ClientSocketUtil.socket.close();
            }
        }
        catch (IOException e) {
            ClientSocketUtil.log.error("socket\u5173\u95ed\u5f02\u5e38", (Throwable)e);
            throw new RuntimeException("socket\u901a\u8baf\u5f02\u5e38", e);
        }
    }
    
    public boolean isConnected() {
        return this.isConnected;
    }
    
    public void setConnected(final boolean isConnected) {
        this.isConnected = isConnected;
    }
    
    public static String readStream(final InputStream is) throws IOException {
        final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        final StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        }
        catch (IOException e) {
            ClientSocketUtil.log.error("readStream\u5f02\u5e38", (Throwable)e);
            throw new RuntimeException("socket\u901a\u8baf\u5f02\u5e38", e);
        }
        finally {
            if (reader != null) {
                try {
                    reader.close();
                }
                catch (Exception e2) {
                    ClientSocketUtil.log.error("IO\u5173\u95ed\u5f02\u5e38", (Throwable)e2);
                    throw new RuntimeException("socket\u901a\u8baf\u5f02\u5e38", e2);
                }
            }
        }
        return sb.toString();
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ClientSocketUtil.class);
    }
}
